<?php

namespace App\Dao;


use App\Constants\CodeData;
use App\Exception\MessageException;
use App\Model\Anchor;
use App\Model\AnchorVideo;
use App\Utils\MemcacheClient;
use Hyperf\Context\ApplicationContext;
use Hyperf\DbConnection\Db;

class AnchorVideoDao
{

    private static $instance;

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = Applicationcontext::getcontainer()->get(self::class);
        }
        return self::$instance;
    }

    /**
     * Note - 根据主播id获取所有视频
     * DateTime 2024/7/16 17:39
     * User Admin
     * @param $id
     * @param $type
     * @param $page
     * @param $pageSize
     * @param $from
     * @param $version_code
     * @param $channel
     * @return array
     */
    public function getAllVideo($id, $type, $page, $pageSize, $from, $version_code, $channel)
    {
        $anchorVideo = MemcacheClient::getInstance()->get('anchor_free_all_video_' . $id);
        $where = [];
        if (empty($anchorVideo)) {
            if ($type == 1) {
                $where[] = ['anchor_id', '=', $id];
            } else {
                if ((new AppUpgradeDao())->isHideCall($from, $version_code, $channel)) {
                    $where[] = ['status', '=', 1];
                    $where[] = ['anchor_id', '=', $id];
                    $where[] = ['is_recommend', '=', 0];
                } else {
                    $where[] = ['status', '=', 1];
                    $where[] = ['anchor_id', '=', $id];
                }
            }

            $anchorVideo = AnchorVideo::select(['id', 'url', 'title', 'anchor_id', 'first_photo', 'is_recommend', 'status', 'click', 'ctime'])->where($where)
                ->orderBy('is_recommend', 'desc')->orderBy('ctime', 'desc')->forPage($page, $pageSize)->get()->toArray();
            // MemcacheClient::getInstance()->set('anchor_free_all_video_' . $id, $anchorVideo, 300);
        }
        return $anchorVideo;
    }


    /**
     * Note - 根据主播id获取免费小视频
     * DateTime 2024/7/19 12:22
     * User Admin
     * @param $uid_list
     * @return array
     */
    public function loadAnchorsVideo($uid_list)
    {
        if (empty($uid_list)) {
            return array();
        }
        // $uids = implode(",", $uid_list);
        // $sql = "select id,anchor_id,url from tb_anchor_video where anchor_id in ($uids) and is_recommend = 0 and status = 1 order by ctime asc";
        // $pd = $this->createCommand($sql);
        // $pd->execute();
        // $rs = $pd->fetchAll(PDO::FETCH_ASSOC);
        $rs = AnchorVideo::select(['id', 'anchor_id', 'url'])
            ->where('anchor_id', 'in', $uid_list)
            ->where('is_recommend', '=', 0)
            ->where('status', '=', 1)
            ->orderBy('ctime', 'asc')
            ->get()->toArray();
        $anchors = [];
        foreach ($rs as $v) {
            $anchors[$v['anchor_id']] = $v;
        }

        return $anchors;
    }


    /**
     * Note - 上传视频
     * DateTime 2024/8/16 16:52
     * User Admin
     * @param $uid
     * @param $first_photo
     * @param $title
     * @param $video_url
     * @param $is_recommend
     * @return array
     */
    public function insert($uid, $first_photo, $title, $video_url, $is_recommend)
    {
        $insert = array();
        $insert['anchor_id'] = $uid;
        $insert['first_photo'] = $first_photo;
        $insert['title'] = $title;
        $insert['url'] = $video_url;
        $insert['android_url'] = $video_url;
        $insert['ios_url'] = $video_url;
        $insert['is_public'] = 0;
        $insert['is_recommend'] = $is_recommend;
        $insert['ctime'] = time();

        //插入成功后返回id
        $photo_id = AnchorVideo::query()->insertGetId($insert);
        if ($photo_id) {
            return formatData(['result' => 1], 200, CodeData::SUCCESS, '操作成功');
        } else {
            return formatData('', 200, CodeData::NEED_TIPS, '上传失败');
        }
    }


    /**
     * Note - 修改视频
     * DateTime 2024/8/16 17:00
     * User Admin
     * @param $id
     * @param $uid
     * @param $is_recommend
     * @return array
     */
    public function update($id, $uid, $is_recommend)
    {
        $ret = AnchorVideo::query()->whereIn('id', $id)->where('anchor_id', $uid)->update(['is_recommend' => $is_recommend]);
        return formatData(['result' => $ret], 200, CodeData::SUCCESS, '视频修改成功');
    }


    /**
     * Note - 删除视频
     * DateTime 2024/8/16 17:00
     * User Admin
     * @param $id
     * @param $uid
     * @return array
     */
    public function delete($id, $uid)
    {
        try {
            DB::beginTransaction();
            $video_info = AnchorVideo::query()->whereIn('id', $id)->where('anchor_id', $uid)->get();
            if ($video_info->isEmpty()) {
                return formatData([], 200, CodeData::NEED_TIPS, '视频不存在');
            }
            $video_url = $video_info->pluck('url')->toArray();
            Anchor::query()->where('uid', $uid)->whereIn('video_url', $video_url)->update(['video_url' => '']);
            $ret = AnchorVideo::query()->whereIn('id', $id)->delete();
            // $ret = $video_info->each->delete();
            // $ret = AnchorVideo::destroy($id);
            DB::commit();
            return formatData(['result' => $ret], 200, CodeData::SUCCESS, '视频删除成功');
        } catch (\Exception $e) {
            DB::rollBack();
            throw new MessageException('修改失败', CodeData::NEED_TIPS, [$e->getMessage(), $e->getCode()]);
        }
    }


}